
local function CastSpell(player)
    local function CastSpell_p(player)
        -- SpawnPrefab("shadow_bishop_fx").Transform:SetPosition(theplayer.Transform:GetWorldPosition()) 
        local CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ player.components.npc_base_lib:Get_Language() ] ["spell_reject_the_npc"] or {}
        if player.__link_npc and player.__link_npc:IsValid() then
            local theNPC = player.__link_npc
            if theNPC:HasDebuff("npc_event_leave") then
                return
            end
            if theNPC:HasTag("Unable_To_Refuse") then
                player.components.npc_base_lib:Wisper(CHAT_TABLE.failure,true)
                return
            end


            if theNPC.npc_event and theNPC:HasDebuff(theNPC.npc_event) then
                local debuff_inst = theNPC:GetDebuff(theNPC.npc_event)
                if debuff_inst then
                    debuff_inst:Remove()
                end
                theNPC.event_step = 100
                theNPC:AddDebuff("npc_event_leave","npc_event_leave")
                player.components.npc_base_lib:Wisper(CHAT_TABLE.success,true)
            end
        else
            -- ----------------------------------------------------------------------------------------------------------
            -- if player._____spell_reject_the_npc_announce_cooldown_task ~= nil then
            --     return
            -- end
            -- player._____spell_reject_the_npc_announce_cooldown_task = player:DoTaskInTime(20,function()
            --     player._____spell_reject_the_npc_announce_cooldown_task = nil
            -- end)
            -- player.components.npc_base_lib:Announce(CHAT_TABLE.failure,true)
            -- ----------------------------------------------------------------------------------------------------------
        end


    end
    local flg,ret = pcall(CastSpell_p,player)    
end

local function builder_onbuilt(inst, builder)
    -- print(inst,builder,"fake error")
	if builder then
        CastSpell(builder)
    end
    inst:Remove()
end

local function fn()

    local inst = CreateEntity()

    inst.entity:AddTransform()

    inst:AddTag("CLASSIFIED")

    inst.persists = false

    inst:DoTaskInTime(0, inst.Remove)

    


    if not TheWorld.ismastersim then
        return inst
    end

   -- inst.OnBuiltFn = builder_onbuilt
   inst:ListenForEvent("onbuilt",function(inst,_table)
        -- print("event : onbuilt")
        if _table and _table.builder then
            builder_onbuilt(inst,_table.builder)
        end
    end)
    return inst
end

return Prefab("spell_reject_the_npc", fn)


